package com.seari.web.controller;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.seari.bean.ArrangementPostBean;
import com.seari.bean.ArrangementPostModelBean;
import com.seari.pojo.MessageBean;

@Controller
public class ArrangementPostController
{
	@Autowired
	com.seari.service.ArrangementPostService ArrangementPostService;
	
	@RequestMapping(value = "post/renderPostID")
	@ResponseBody
	public void postTest() {
//		ArrangementPostService.renderArrangementPostInfo();
//		System.out.println("success");
	}
	
	@RequestMapping(value = "post/renderPostEvent")
	@ResponseBody
	public MessageBean renderPostEvent(String start) {
		MessageBean mb = new MessageBean();
		try
		{
 			List<ArrangementPostBean> postInfo = ArrangementPostService.renderArrangementPostInfo(start);
			mb.setCode("success");
			mb.setData(postInfo);
		} catch (Exception e)
		{
			mb.setCode("error");
			mb.setMessage("获取布岗信息执行异常...");
		}
		return mb;
	}
	
	@RequestMapping(value = "post/addPostEvent")
	@ResponseBody
	public MessageBean addPostEvent(ArrangementPostBean req) {
		MessageBean mb = new MessageBean();
		if(null == req.getResourceId() || null == req.getStart() || null == req.getEnd() || null == req.getPostValue() || null == req.getPostIdValue() || null == req.getAreaValue())
        {
            //表单验证
            mb.setCode("warning");
            mb.setMessage("表单信息不完整");
            return mb;
        }
		try
		{
			List<Integer> ids =  ArrangementPostService.checkPostEventExists(req.getResourceId(), req.getStart(), req.getEnd());
			if(ids.size() > 0) {
				mb.setCode("warning");
	            mb.setMessage("该时间段已存在布岗任务，请重新选择！");
	            return mb;
			}
			int result = ArrangementPostService.insertArrangementPostInfo(req);
			if(result > 0) {
				mb.setCode("success");
				mb.setMessage("布岗事件添加成功!");
			}else {
				mb.setCode("error");
				mb.setMessage("布岗事件添加失败!");
			}
		} catch (Exception e)
		{
			mb.setCode("error");
			mb.setMessage("新增布岗信息执行异常...");
		}
		return mb;
	}
	
	@RequestMapping(value = "post/delPostEvent")
	@ResponseBody
	public MessageBean delPostEvent(int pid) {
		MessageBean mb = new MessageBean();
		if(pid == 0) {
			mb.setCode("warning");
			mb.setMessage("布岗事件ID缺失！");
			return mb;
		}
		try
		{
			int num = ArrangementPostService.deletePostEvent(pid);
			if(num < 0) {
				mb.setCode("error");
	            mb.setMessage("删除布岗事件失败！");
			}else {
				mb.setCode("success");
	            mb.setMessage("删除布岗事件成功！");
			}
		} catch (Exception e)
		{
			mb.setCode("error");
            mb.setMessage("删除布岗任务执行异常！");
		}
		return mb;
	}
	
	@RequestMapping(value = "post/updatePostEvent")
	@ResponseBody
	public MessageBean updatePostEvent(ArrangementPostBean req) {
		MessageBean mb = new MessageBean();
		if(null == req.getResourceId() || null == req.getStart() || null == req.getEnd() || null == req.getPostValue() || null == req.getPostIdValue() || null == req.getAreaValue())
        {
            //表单验证
            mb.setCode("warning");
            mb.setMessage("表单信息不完整");
            return mb;
        }
		try
		{
			List<Integer> ids =  ArrangementPostService.checkPostEventExists(req.getResourceId(), req.getStart(), req.getEnd());
			if(ids.size() > 1 || (ids.size() == 1 && !req.getId().equals(ids.get(0)))) {
				mb.setCode("warning");
	            mb.setMessage("该时间段已存在布岗任务，请重新选择！");
	            return mb;
			}
			int result = ArrangementPostService.updatePostEvent(req);
			if(result > 0) {
				mb.setCode("success");
				mb.setMessage("布岗事件修改成功!");
			}else {
				mb.setCode("error");
				mb.setMessage("布岗事件修改失败!");
			}
		} catch (Exception e)
		{
			mb.setCode("error");
			mb.setMessage("修改布岗信息执行异常...");
		}
		return mb;
	}
	
	@RequestMapping(value = "post/modelPostEvent")
	@ResponseBody
	public MessageBean renderModelPostEvent(String modelName) {
		MessageBean mb = new MessageBean();
		try
		{
 			List<ArrangementPostBean> postInfo = ArrangementPostService.renderModelPostEvent(modelName);
			mb.setCode("success");
			mb.setData(postInfo);
		} catch (Exception e)
		{
			mb.setCode("error");
			mb.setMessage("获取布岗模板数据执行异常...");
		}
		return mb;
	}
	
	@RequestMapping(value = "post/modelPostEventHis")
	@ResponseBody
	public MessageBean renderModelPostEventHis(String date,String modelName) {
		MessageBean mb = new MessageBean();
		try
		{
 			List<ArrangementPostBean> postInfo = ArrangementPostService.renderModelPostEventHis(date, modelName);
			mb.setCode("success");
			mb.setData(postInfo);
		} catch (Exception e)
		{
			mb.setCode("error");
			mb.setMessage("获取布岗模板数据执行异常...");
		}
		return mb;
	}
	
	@RequestMapping(value = "post/modelName")
	@ResponseBody
	public MessageBean fecthPostModelNames() {
		MessageBean mb = new MessageBean();
		try
		{
			List<ArrangementPostModelBean> modelBean = ArrangementPostService.fetchPostModel();
			Map<String,String> res = new HashMap<>();
			for (ArrangementPostModelBean arrangementPostModelBean : modelBean)
			{
				res.put(arrangementPostModelBean.getModelId(), arrangementPostModelBean.getModelName());
			}
			mb.setCode("success");
			mb.setData(res);
		} catch (Exception e)
		{
			mb.setCode("error");
			mb.setMessage("获取布岗模板名称执行异常...");
		}
		return mb;
	}
	
	@RequestMapping(value = "post/confirmModel")
	@ResponseBody
	public MessageBean confirmPostModel(String modelId) {
		MessageBean mb = new MessageBean();
		try
		{
			ArrangementPostService.confirmPostModel(modelId);
			mb.setCode("success");
		} catch (Exception e)
		{
			mb.setCode("error");
			mb.setMessage("确认布岗模板执行异常...");
		}
		return mb;
	}
}
